<?
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('menu.inc');

function SaveNewTm() {
	$kt_num = $_REQUEST['kt_num'];
	$kt_tm = $_REQUEST['kt_tm'];

	$db = OpenDB();
	$query = "select kt_tm from key_tmgroup where kt_num = $kt_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows == 1) {
	   Error("Schl&uuml;ssel $kt_num ist bereits vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "insert into key_tmgroup (kt_num, kt_tm) values ($kt_num, '$kt_tm')";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

function SaveEditTm() {
	$kt_num = $_REQUEST['kt_num'];
	$kt_tm = $_REQUEST['kt_tm'];

	$db = OpenDB();
	$query = "select kt_tm from key_tmgroup where kt_num = $kt_num";
	$result = QueryDB($db, $query);
	$numrows = numrowsDB($result);

	if ($numrows != 1) {
	   Error("Schl&uuml;ssel $kt_num ist nicht vorhanden!\n");
	   closeDB($db);
	   return false;
	}

	$query = "update key_tmgroup set kt_tm = '$kt_tm' where kt_num = $kt_num";

	if (!TQueryDB($db, $query)) {
	   Error("SQL-Error: $query");
	   closeDB($db);
	   return false;
	}

	closeDB($db);
	return true;
}

$newtmgroup = $_REQUEST['newtmgroup'];
$edittmgroup = $_REQUEST['edittmgroup'];

if (!isset($edittmgroup)) {
   $edittmgroup = 0;
}

$savenewtm = $_REQUEST['savenewtm'];
$saveedittm = $_REQUEST['saveedittm'];

if (isset($savenewtm)) {
   SaveNewTm();
}

if (isset($saveedittm)) {
   SaveEditTm();
}

?>
<form action="tmgroup.php" method="post">
<input type="hidden" name="headline" value="14">
<input type="hidden" name="rstufe" value="<? echo "$rstufe"; ?>">
<input type="hidden" name="menu" value="<? echo "$menu"; ?>">
<input type="hidden" name="unum" value="<? echo "$unum"; ?>">
<button type="submit" name="newtm" value="newtmgroup"><img src="image/button_newtmgroup.png" border=0 alt="Neue TM-Gruppe"></button>
<?php
$db = OpenDB();
$query = "select kt_num, kt_tm from key_tmgroup order by kt_num";
$result = QueryDB($db, $query);

if (!$result) {
   echo "</table></form><br>\n";
   Error("SQL-Error: $query");
   require('footer.inc');
   exit;
}

$numrows = numrowsDB($result);

if ($numrows > 0 || isset($newtmgroup)) {
?>
<table border=1>
   <tr>
      <td class="tbhead">&nbsp;</td>
      <td class="tbhead">Key</td>
      <td class="tbhead">Text</td>
   </tr>
<?php
   $row = 0;

   while ($row < $numrows) {
      $data = fetchDB($result, $row);
      $kt_num = $data[0];
      $kt_tm = $data[1];

      if ($kt_num == $edittmgroup) {
         echo "<input type=\"hidden\" name=\"kt_num\" value=\"$kt_num\">\n";
         echo "<tr><td><button type=\"submit\" name=\"saveedittm\" value=\"$kt_num\"><img src=\"image/new.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kt_num</td>";
         echo "<td><input type=\"text\" name=\"kt_tm\" value=\"$kt_tm\" size=40 maxlength=50></td></tr>\n";
      } else {
         echo "<tr><td><button type=\"submit\" name=\"edittmgroup\" value=\"$kt_num\"><img src=\"image/edit.png\" border=0 alt=\"Edit\"></button></td>";
         echo "<td>$kt_num</td><td>$kt_tm</td></tr>";
      }

      $row++;
   }

   if (isset($newtmgroup)) {
      echo "<tr><td><button type=\"submit\" name=\"savenewtm\" value=\"savenewtm\"><img src=\"image/new.png\" border=0 alt=\"Neu\"></button></td>";
      echo "<td><input type=\"text\" name=\"kt_num\" size=7 maxlength=7></td>";
      echo "<td><input type=\"text\" name=\"kt_tm\" size=40 maxlength=50></td></tr>\n";
   }
?>
</table>
<button type="submit" name="newtmgroup" value="new"><img src="image/button_newtmgroup.png" border=0 alt="Neue TM-Gruppe"></button>
</form>
<?php
}

require('footer.inc');
?>
